<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>computed与watch的对比</title>
    <script src="/js/vue.js"></script>
</head>
<body>
    <div id="app"></div>

</body>

<script>
   let vm=new Vue({
      el:'#app',
      data:{
        firstName: 'Foo',
        lastNmae: 'Bar',
        // fullName: 'Foo Bar'  //验证计算属性时，本语句需要注释掉
      },
      watch:{
        firstName: function(val){
            this.fullName = val + ' ' + this.lastNmae;
        },
        lastNmae: function (val) {
            this.fullName = this.firstName + ' ' + val;
        }
      },
      computed: {
        fullName: {
            //getter
            get: function(){
                return this.firstName + ' ' + this.lastNmae;
            },
            //setter
            set: function (newValue) {
                var names = newValue.split(' ');
                this.firstName = names[0];
                this.lastNmae = names[names.length - 1];
            }
        }
      }
   })
</script>

</html>